//
//  ProtocolService.h
//  healthApp
//
//  Created by william mu on 12-8-28.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <SBJson/SBJson.h>
#import "GlobalState.h"


NSDictionary* translate_health_info(NSString* json)
{    
    NSDictionary* dict = (NSDictionary*) [json JSONValue];
    
    GlobalState* state = [GlobalState sharedInstance];
    
    NSDictionary* healthFieldMap = state.healthFieldMap;
    NSMutableDictionary* result = [NSMutableDictionary dictionary];
    
    for(NSString* validField in healthFieldMap)
    {
        NSString* value = [dict objectForKey:validField];
        [result setObject:value forKey:validField];
    }
    
    return result;
}

NSDictionary* translate_user_base_info(NSString* json)
{
    NSDictionary* dict = (NSDictionary*)[json JSONValue];

    
    return dict;
}

NSArray* translate_symptom(NSString* json)
{
    
    NSArray* ary = (NSArray*)[json JSONValue];
    
    
    return ary;
}


NSString* get_short_desc(NSString* str, int maxLen)
{
    if([str length] > maxLen){
        str = [str substringToIndex:maxLen - 1];
        
        str = [str stringByAppendingString:@"..."];
    }
    
    return str;
}

NSString* get_symptoms_desc(NSArray* symptoms, int maxLen)
{
    NSString* final = @"";
    
    GlobalState* state = [GlobalState sharedInstance];
    
    for(NSString* s in symptoms)
    {
        NSString* symptomCode = [s objectForKey:@"symptomType"];
        NSString* symptomDesc = [state mapSymptomCode2Name:symptomCode];
        final = [final stringByAppendingString:symptomDesc];
        final = [final stringByAppendingString:@"、"];
    }
    
    if([final length] > 0)
        final = [final substringToIndex:[final length] - 1];
    
    return get_short_desc(final, maxLen);
}